<template>
  <div class="child2">
    <h3>子组件2</h3>
		<h4>弟弟有{{ computer }}</h4>
		<h4 v-show="toy">哥哥给的{{ toy }}</h4>
  </div>
</template>

<script setup lang="ts" name="Child2">
	import emitter from '@/utils/emitter';
	import { ref,onUnmounted } from 'vue';
	
	let computer = ref('联想')
	let toy = ref('')
	// 给emitter绑定send-toy事件
	emitter.on('send-toy',(value:any)=>{
		toy.value=value
	})

	// 组件卸载时解绑send-toy事件,避免造成内存泄漏
	onUnmounted(()=>{
		emitter.off('send-toy')
	})

</script>

<style scoped>
	.child2{
		margin-top: 50px;
		background-color: orange;
		padding: 10px;
		box-shadow: 0 0 10px black;
		border-radius: 10px;
	}
</style>